<?php

if (! defined('LIB_PATH'))
{
    die('The file ' . __FILE__ . ' needs to be required from the main executable of PhpPhoenix');
}

define('T_USER_TEXT', 10001);
define('T_USER_BRACE_OPEN', 10002);
define('T_USER_BRACE_CLOSE', 10003);
define('T_USER_PARENTHESES_OPEN', 10004);
define('T_USER_PARENTHESES_CLOSE', 10005);
define('T_USER_SEMICOLON', 10006);
define('T_USER_ASSIGNMENT', 10007);
define('T_USER_COMMA', 10008);
define('T_USER_AMPERSAND', 10009);

/**
 * Tokenizer 
 * 
 * @package php-phoenix
 * @subpackage core
 * @version $id$
 * @copyright 
 * @author Pablo López Torres <pablolopeztorres@gmail.com> 
 * @license 
 */
class Tokenizer
{
    /**
     * prepare 
     * 
     * @param mixed $sourceCode 
     * @static
     * @author Pablo López Torres <pablolopeztorres@gmail.com> 
     * @access public
     * @return void
     */
    public static function prepare($sourceCode)
    {
        $tokens = token_get_all($sourceCode);

        foreach ($tokens as $key => &$token)
        {
            if (is_string($token))
            {
                switch ($token)
                {
                case '{':
                    $tokenId = T_USER_BRACE_OPEN;
                    break;
                case '}':
                    $tokenId = T_USER_BRACE_CLOSE;
                    break;
                case '(':
                    $tokenId = T_USER_PARENTHESES_OPEN;
                    break;
                case ')':
                    $tokenId = T_USER_PARENTHESES_CLOSE;
                    break;
                case '=':
                    $tokenId = T_USER_ASSIGNMENT;
                    break;
                case ',':
                    $tokenId = T_USER_COMMA;
                    break;
                case ';':
                    $tokenId = T_USER_SEMICOLON;
                    break;
                case '&':
                    $tokenId = T_USER_AMPERSAND;
                    break;
                default:
                    $tokenId = T_USER_TEXT;
                    break;
                }
                $token = array(
                    0 => $tokenId, // ID
                    1 => $token, // TEXT
                    2 => NULL, // LINE
                );
            }



            // at this point the token should has been converted to an array
            switch ($token[0])
            {
                case T_COMMENT:
                case T_WHITESPACE:
                    unset($tokens[$key]);
                    break;
                case T_DOC_COMMENT:
                    break;
                default:
                    break;
            }
        }

        reset($tokens);

        return $tokens;
    }

    public static function getConstantName($constant)
    {
        switch ($constant) {
            case T_USER_TEXT:
                $tokenName = 'T_USER_TEXT';
                break;
            case T_USER_BRACE_OPEN:
                $tokenName = 'T_USER_BRACE_OPEN';
                break;
            case T_USER_BRACE_CLOSE:
                $tokenName = 'T_USER_BRACE_CLOSE';
                break;
            case T_USER_PARENTHESES_OPEN:
                $tokenName = 'T_USER_PARENTHESES_OPEN';
                break;
            case T_USER_PARENTHESES_CLOSE:
                $tokenName = 'T_USER_PARENTHESES_CLOSE';
                break;
            case T_USER_SEMICOLON:
                $tokenName = 'T_USER_SEMICOLON';
                break;
            case T_USER_ASSIGNMENT:
                $tokenName = 'T_USER_ASSIGNMENT';
                break;
            case T_USER_COMMA:
                $tokenName = 'T_USER_COMMA';
                break;
            case T_USER_AMPERSAND:
                $tokenName = 'T_USER_AMPERSAND';
                break;
            default:
                $tokenName = token_name($constant);
                break;
        }

        return $tokenName;
    }
}
